package cn.tedu.csmall.server.service.impl;

import cn.tedu.csmall.server.exception.ServiceException;
import cn.tedu.csmall.server.mapper.CategoryMapper;
import cn.tedu.csmall.server.pojo.dto.CategoryAddNewDTO;
import cn.tedu.csmall.server.pojo.dto.CategoryEditDTO;
import cn.tedu.csmall.server.pojo.entity.Category;
import cn.tedu.csmall.server.pojo.vo.BrandDetailVO;
import cn.tedu.csmall.server.pojo.vo.CategoryVO;
import cn.tedu.csmall.server.repo.ICategoryRepository;
import cn.tedu.csmall.server.service.ICategoryService;
import cn.tedu.csmall.server.web.JsonResult;
import cn.tedu.csmall.server.web.ServiceCode;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.rowset.serial.SerialException;
import java.util.List;

/**
 * @author lixin
 * @Date 2022/8/22
 */
@Service
//@Scope("prototype") 原型模式  @Scope("singleton")单例模式创建对象默认单例模式
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private CategoryMapper mapper;

    @Override
    public void addNew(CategoryAddNewDTO categoryAddNewDTO) {
        String name = categoryAddNewDTO.getName();
        Long parentId = categoryAddNewDTO.getParentId();
        int count = mapper.countByName(name);
        if (count > 0) {
            throw new ServiceException(ServiceCode.ERR_CONFLICT, "品牌已存在,不可重复添加");
        }
        final Category category = new Category();
        BeanUtils.copyProperties(categoryAddNewDTO, category);
        //在父级类别深度上+1，没有父级默认深度1
        if (parentId == 0) {
            category.setDepth(1);
        } else {
            CategoryVO parentCategory = mapper.getById(parentId);
            if (parentCategory == null) {
                String err = "添加类别失败,服务器繁忙";
                throw new ServiceException(ServiceCode.ERR_NOT_FOUND, err);
            }
            Integer depth = parentCategory.getDepth();
            category.setDepth(depth + 1);
            //设置父级的is_parent
            if (parentCategory.getIsParent() == 0) {
                Category c = new Category();
                c.setIsParent(1);
                c.setId(parentId);
                int rows = mapper.updateById(c);
                if (rows != 1) {
                    String err = "添加类别失败,服务器繁忙";
                    throw new ServiceException(ServiceCode.ERR_UPDATE, err);
                }
            }
        }
        category.setIsParent(0);
        final int row = mapper.insert(category);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "服务器繁忙，添加失败");
        }
    }


    @Override
    public List<CategoryVO> list() {
        List<CategoryVO> list = mapper.list();
        return list;
    }

    @Override
    public void update(CategoryEditDTO categoryEditDTO) {
        Category category = new Category();
        BeanUtils.copyProperties(categoryEditDTO, category);
        int rows = mapper.updateById(category);
        if (rows != 1) {
            String err = "添加类别失败,服务器繁忙";
            throw new ServiceException(ServiceCode.ERR_UPDATE, err);
        }
    }

    @Override
    public void deleteById(Long id) {
        final CategoryVO categoryVO = mapper.getById(id);
        if (categoryVO == null) {
            throw new ServiceException(ServiceCode.ERR_DELETE, "数据异常，没有该条数据,id=" + id);
        }
        //检查当前类别是否有子类
        {
            int rows = mapper.countByParentId(categoryVO.getId());
            if (rows != 0) {
                throw new ServiceException(ServiceCode.ERR_UPDATE, "删除失败，当前类别还有子类");
            }

        }


        final int row = mapper.deleteById(id);
        if (row != 1) {
            throw new ServiceException(ServiceCode.ERR_UNKNOWN, "服务器繁忙,删除失败id=" + id);
        }
        //检查父类是否还有子类
        Long parentId = categoryVO.getParentId();
        int count = mapper.countByParentId(parentId);
        if (count == 0) {
            Category category = new Category();
            category.setIsParent(0);
            category.setId(parentId);
            int rows = mapper.updateById(category);
            if (rows != 1) {
                throw new ServiceException(ServiceCode.ERR_UPDATE, "服务器繁忙,删除失败id=" + id);
            }
        }


    }

}
